package com.ar.studyapp;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.ar.bean.Exam;

public abstract class BaseActivity extends SherlockFragmentActivity implements OnSharedPreferenceChangeListener {
	protected static boolean didStart = false;

	private boolean settingsChanged = false;
	private boolean willHotUpdateSetting = false;
	private boolean willHideAllBarItem = true;
	private static String currentCategoryId;
	private static String currentDifficultId;
	private static String currentSubjectName;
	private static String currentDiffName;
	private static Exam data;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getSettings();
		if (didStart == false) {
			didStart = true;
			doPreparationProcessOfApp();
		}
		displayActivity();
		initVariables();
		initViews();
		initActions();
		setUpActionBar();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(willHotUpdateSetting) if (settingsChanged) updateSettings();
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		if (willHideAllBarItem){
			menu.findItem(R.id.item_check).setVisible(false);
			menu.findItem(R.id.item_time_count_down).setVisible(false);
		}
		return super.onPrepareOptionsMenu(menu);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
        final com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * SetContentView, display setting and so on..
	 */
	protected abstract void displayActivity();

	/**
	 * initialize Views
	 */
	protected abstract void initViews();

	/**
	 * initialize global variables
	 */
	protected abstract void initVariables();

	/**
	 * beginning actions of the Activity
	 */
	protected abstract void initActions();

	/**
	 * Scan files from sdcard then read content of those.</br> Finally, save
	 * result of those processes into some collections.</br> Those are data of
	 * the application. Thus, write once, use the data everywhere.
	 */
	private void doPreparationProcessOfApp() {
		//sau này tự viết sau
	}
	

	private void getSettings() {
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
	
	protected void restartActivity(){
		Intent thisIntent = getIntent();
		finish();
		startActivity(thisIntent);
	}
	
	private void updateSettings(){
		//Sau này viết lại vào đây
	}
	
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		settingsChanged = true;
	}
	
	protected void setWillHotUpdateSettings(boolean willHotUpdateSettings) {
		this.willHotUpdateSetting = willHotUpdateSettings;
	}
	
	protected void setUpActionBar(){
		getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#dc5d54")));
		//ActionBar bar = getSupportActionBar();
		//bar.setDisplayHomeAsUpEnabled(true);
		//bar.setHomeButtonEnabled(true);
	}
	
	public void setHideActionBarItems(boolean value){
		this.willHideAllBarItem = value;
	}
	
	public Activity getActivityInstance(){
		return this;
	}
	
	public void setCurrentCategoryId(String id){
		currentCategoryId = id;
	}
	
	
	public String getCurrentCategoryId(){
		return currentCategoryId;
	}
	
	public void setCurrentDifficultId(String id){
		currentDifficultId = id;
	}
	
	
	public String getCurrentDifficultId(){
		return currentDifficultId;
	}
	
	public void setCurrentSubjectName(String subjName){
		currentSubjectName = subjName;
	}
	
	public String getCurrentSubjectName(){
		return currentSubjectName;
	}
	
	public void setCurrentDiffName(String diffName){
		currentDiffName = diffName;
	}
	
	public String getCurrentDiffName(){
		return currentDiffName;
	}

	/**
	 * @return the data
	 */
	public static Exam getExam() {
		return data;
	}

	/**
	 * @param data the data to set
	 */
	public static void setExam(Exam data) {
		BaseActivity.data = data;
	}

}
